package com.ybfenpan.taglib.pagetag;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class Pagination implements Serializable{
	
	public static String pageBeanName = "pagination";
	
	// 默认页面显示条目数
	public static final int DEFULT_PAGE_SIZE = 20;
    
	/*
	 * 总记录数
	 */
	private Integer totalCount = 0;
	
	/**
     * 当前页面序号
     */
    private int page;
    
    /**
     * 页面总数
     */
    private int pageCount;
    
    /**
     * 页面尺寸
     */
    private int pageSize = DEFULT_PAGE_SIZE;

    /**
     * 起始的记录序号
     */
    private int startRowNum;
    
    /**
     * 终了记录序号
     */
    private int endRowNum;
    
    
	/**
     * 排序
     */
    private String orderBy;
	
	/**
	 * 条目
	 */
	private List pageList = new ArrayList();
	
	
	/**
     * Determines if the isFirstPage property is true.
     * @return <code>true<code> if the isFirstPage property is true
     */
    public boolean getIsFirstPage() {
        return page <= 1 ? true : false;
    }

    /**
     * Determines if the isLastPage property is true.
     * @return <code>true<code> if the isLastPage property is true
     */
    public boolean getIsLastPage() {
        return pageCount <= page ? true : false;
    }
    

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	/**
     * Access method for the currentPage property.
     * @return the current value of the currentPage property
     */
    public int getPage() {
        // 判断总记录数大于零且当前也是小于一的情况
        if (page < 1 && totalCount > 0) {
        	page = 1;
        }
        // 判断当前页序号是否溢出
        if (page > getPageCount()) {
        	page = pageCount;
        }
        return page;
    }
    
    /**
     * Sets the value of the currentPage property.
     * @param aCurrentPage the new value of the currentPage property
     */
    public void setPage(int aCurrentPage) {
        // 设置当前页序号、小于零的情况忽略
        if (aCurrentPage >= 0) {
        	page = aCurrentPage;
        }
    }
    
    /**
     * Access method for the pageCount property.
     * @return the current value of the pageCount property
     */
    public int getPageCount() {
        // 判断记录总数是否能整除页尺寸
        if (totalCount % pageSize == 0) {
            // 整除则直接取整相除
            pageCount = (totalCount / pageSize);
        } else {
            // 否则取整相除后加一
            pageCount = (totalCount / pageSize) + 1;
        }
        return pageCount;
    }
    
    /**
     * Sets the value of the pageCount property.
     * @param aPageCount the new value of the pageCount property
     */
    public void setPageCount(int aPageCount) {
        pageCount = aPageCount;
    }
    
    /**
     * Access method for the pageSize property.
     * @return the current value of the pageSize property
     */
    public int getPageSize() {
        return pageSize;
    }
    
    /**
     * Sets the value of the pageSize property.
     * @param aPageSize the new value of the pageSize property
     */
    public void setPageSize(int aPageSize) {
        pageSize = aPageSize;

    }
    
    /**
     * Access method for the endRowNum property.
     * @return the current value of the endRowNum property
     */
    public int getEndRowNum() {
        // 判断记录总数是否能整除页尺寸
        if (page > getPageCount()) {
        	page = pageCount;
        }
        // 如果当前页小于一则结束序号为页面大小，否则按公式计算
        endRowNum = (page - 1) > 0 ? (page - 1) * pageSize + pageSize : pageSize;
        return endRowNum;
    }
    
    /**
     * Sets the value of the endRowNum property.
     * @param aEndRowNum the new value of the endRowNum property
     */
    public void setEndRowNum(int aEndRowNum) {
        endRowNum = aEndRowNum;
    }
    
    /**
     * Access method for the startRowNum property.
     * @return the current value of the startRowNum property
     */
    public int getStartRowNum() {
        // 判断记录总数是否能整除页尺寸
        if (page > getPageCount()) {
        	page = pageCount;
        }
        startRowNum = ((page - 1) * pageSize > 0 ? (page - 1) * pageSize : 0);
        return startRowNum;
    }
    
    /**
     * Sets the value of the startRowNum property.
     * @param aStartRowNum the new value of the startRowNum property
     */
    public void setStartRowNum(int aStartRowNum) {
        startRowNum = aStartRowNum;
    }
    
	/**
    * @Title: getOrderBy
    * @return String
    */
    public String getOrderBy() {
        if ("null".equalsIgnoreCase(orderBy)) {
            orderBy = "";
        }
        return orderBy;
    }
    
    /**
     * @Title: setOrderBy
     * @param orderBy orderBy
     */
     public void setOrderBy(String orderBy) {
         this.orderBy = orderBy;
     }
     
	public List getPageList() {
		return pageList;
	}

	public void setPageList(List pageList) {
		this.pageList = pageList;
	}
	
	public int getStartRow(){
		int p = getPage();
		p = p < 1 ? 1 : p;
		int ps = getPageSize();
		ps = ps < 1 ? 1 : ps;
		return (p-1)*ps;
	}
	
	public int getEndRow(){
		int p = getPage();
		p = p < 1 ? 1 : p;
		int ps = getPageSize();
		ps = ps < 1 ? 1 : ps;
		return p*ps;
	}

	public static String getPageBeanName() {
		return pageBeanName;
	}

	public static void setPageBeanName(String pageBeanName) {
		Pagination.pageBeanName = pageBeanName;
	}


}
